<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" ghost @click="handleExportExcel()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="ant-design:vertical-align-bottom-outlined" /> {{ t('导入') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <AddQtyModal @register="registerUseQtyModal" @success="handleSuccess" />
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasMoldBasMoldList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { basMoldDelete, basMoldListData } from '/@/api/bas/mold/basMold';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import AddQtyModal from './addQtyModal.vue';
  import { downloadByUrl } from '/@/utils/file/download';
  import { basMoldClsTreeData } from '/@/api/bas/mold/basMoldCls';
  import PrintModal from '/@/components/print/printModal.vue';

  const props = defineProps({
    treeCode: String,
    treeName: String,
  });

  const { t } = useI18n('bas.mold.basMold');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('工装档案管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    autoAdvancedLine: 10,
    schemas: [
      {
        label: t('工装代码'),
        field: 'caddcode',
        component: 'Input',
      },
      {
        label: t('工装编码'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('工装名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('工装类型'),
        field: 'clsCode',
        fieldLabel: 'basMold.basMoldCls.treeName',
        component: 'TreeSelect',
        componentProps: {
          api: basMoldClsTreeData,
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('工装状态'),
        field: 'moldStatus',
        component: 'Select',
        componentProps: {
          dictType: 'bas_mold_status',
          onChange: handleSuccess,
          allowClear: true,
        },
      },
      {
        label: t('存货代码'),
        field: 'basInv.cinvaddcode',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'basInv.cinvcode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
      {
        label: t('工序'),
        field: 'operCode',
        component: 'Input',
      },
      {
        label: t('位置名称'),
        field: 'locationName',
        component: 'Input',
      },
      {
        label: t('部门名称'),
        field: 'depName',
        component: 'Input',
      },
      {
        label: t('供应商名称'),
        field: 'venName',
        component: 'Input',
      },
      {
        label: t('使用日期'),
        field: 'useDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
    ],
    fieldMapToTime: [
      ['buyDate', ['buyDate_gte', 'buyDate_lte']],
      ['makeDate', ['makeDate_gte', 'makeDate_lte']],
      ['installDate', ['installDate_gte', 'installDate_lte']],
      ['checkDate', ['checkDate_gte', 'checkDate_lte']],
      ['useDate', ['useDate_gte', 'useDate_lte']],
      ['expiryDate', ['expiryDate_gte', 'expiryDate_lte']],
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('工装编码'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 180,
      align: 'left',
      fixed: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('工装尾号'),
      dataIndex: 'lastNum',
      key: 'a.lastNum',
      sorter: true,
      width: 90,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('工装名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 130,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('工装代码'),
      dataIndex: 'caddcode',
      key: 'a.caddcode',
      sorter: true,
      width: 130,
      align: 'left',
      //fixed: 'left',
    },
    {
      title: t('工装状态'),
      dataIndex: 'moldStatus',
      key: 'a.mold_status',
      sorter: true,
      dictType: 'bas_mold_status',
      width: 100,
      align: 'center',
      //fixed: 'left',
    },
    {
      title: t('工装类型'),
      dataIndex: 'basMoldCls.name',
      key: 'cls.code',
      sorter: true,
      width: 100,
      align: 'center',
      //fixed: 'left',
    },
    {
      title: t('存货代码'),
      dataIndex: 'basInv.cinvaddcode',
      width: 150,
      align: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'basInv.cinvcode',
      key: 'inv.cinvcode',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('工序'),
      dataIndex: 'operCode',
      key: 'a.oper_code',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('所在位置编码'),
      dataIndex: 'locationCode',
      key: 'a.location_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('所在位置名称'),
      dataIndex: 'locationName',
      key: 'a.location_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('所在部门编码'),
      dataIndex: 'depCode',
      key: 'a.dep_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('所在部门名称'),
      dataIndex: 'depName',
      key: 'a.dep_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('是否共模'),
      dataIndex: 'bcomomMode',
      key: 'a.bcomom_mode',
      sorter: true,
      dictType: 'sys_yes_no',
      width: 100,
      align: 'center',
    },
    {
      title: t('供应商名称'),
      dataIndex: 'venName',
      key: 'a.ven_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('购买日期'),
      dataIndex: 'buyDate',
      key: 'a.buy_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('制作日期'),
      dataIndex: 'makeDate',
      key: 'a.make_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('安装日期'),
      dataIndex: 'installDate',
      key: 'a.install_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('验收日期'),
      dataIndex: 'checkDate',
      key: 'a.check_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('使用日期'),
      dataIndex: 'useDate',
      key: 'a.use_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('保修截止日期'),
      dataIndex: 'expiryDate',
      key: 'a.expiry_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('创建人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('创建时间'),
      dataIndex: 'creatDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('设计寿命'),
      dataIndex: 'iquantity',
      key: 'a.iquantity',
      sorter: true,
      width: 130,
      align: 'right',
      //fixed: 'right',
    },
    {
      title: t('可用次数'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 100,
      align: 'right',
      //fixed: 'right',
    },
    {
      title: t('累计加工次数'),
      dataIndex: 'useQty',
      key: 'a.use_qty',
      sorter: true,
      width: 100,
      align: 'right',
      showSummary: true,
      //fixed: 'right',
    },
    {
      title: t('追加次数'),
      dataIndex: 'addQty',
      key: 'a.add_qty',
      sorter: true,
      width: 100,
      align: 'right',
      //fixed: 'right',
    },
    {
      title: t('保养预警次数'),
      dataIndex: 'byQty',
      key: 'a.by_qty',
      sorter: true,
      width: 100,
      align: 'right',
      //fixed: 'right',
    },
    {
      title: t('点检人'),
      dataIndex: 'checker',
      key: 'a.checker',
      sorter: true,
      width: 130,
      align: 'left',
      //fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 100,
    actions: (record: Recordable) => [
      {
        icon: 'ant-design:printer-outlined',
        title: t('打印'),
        onClick: handlePrint.bind(this, { code: record.code, lastNum: record.lastNum }),
        auth: 'bas:mold:basMold:edit',
      },
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑工装档案'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'bas:mold:basMold:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除工装档案'),
        popConfirm: {
          title: t('是否确认删除工装档案'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'bas:mold:basMold:delete',
      },
    ],
  };

  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: basMoldListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    // rowSelection: {
    //   type: 'checkbox',
    // },
  });

  watch(
    () => props.treeCode,
    async () => {
      await getForm().setFieldsValue({
        clsCode: props.treeCode,
        'basMold.basMoldCls.treeName': props.treeName,
      });
      reload();
    },
  );

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/bas/mold/basMold/exportData',
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  function handleExportExcel() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/bas/mold/basMold/exportData',
      params: {
        ...getForm().getFieldsValue(),
      },
      target: '_self',
    });
  }

  async function handleDelete(record: Recordable) {
    const res = await basMoldDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  function handlePrint(record: Recordable) {
    const ids = record.code + '@' + record.lastNum;
    let obj = {
      title: '模具标签打印',
      ids: ids,
      height: '0.7',
      width: '0.6',
      fileName: '工装档案打印',
    };
    openPrintModal(true, obj);
  }
</script>
